package com.example.demo.huawei;

import java.util.Scanner;

/*
计算三叉搜索数高度
 */
public class OD60 {
    static class TreeNode {
        int value;
        TreeNode left;
        TreeNode middle;
        TreeNode right;

        public TreeNode(int value) {
            this.value = value;
            this.left = null;
            this.right = null;
            this.middle = null;
        }
    }

    public static TreeNode insertNode(TreeNode root, int value) {
        if (root == null) {
            return new TreeNode(value);
        }
        if (value < root.value - 500) {
            root.left = insertNode(root.left, value);
        } else if (value > root.value + 500) {
            root.right = insertNode(root.right, value);
        } else {
            root.middle = insertNode(root.middle, value);
        }
        return root;
    }

    public static TreeNode constructBST(int[] values) {
        TreeNode root = null;
        for (int value : values) {
            root = insertNode(root, value);
        }
        return root;
    }

    public static void inorderTraversal(TreeNode root) {
        if (root != null) {
            inorderTraversal(root.left);
            System.out.print(root.value + " ");
            inorderTraversal(root.right);
        }
    }

    public static int calculateHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftHeight = calculateHeight(root.left);
        int rightHeight = calculateHeight(root.right);
        int middleHeight = calculateHeight(root.middle);
        return Math.max(middleHeight, Math.max(leftHeight, rightHeight)) + 1;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] values = new int[n];
        for (int i = 0; i < n; i++) {
            values[i] = scanner.nextInt();

        }
        TreeNode tree = constructBST(values);
        System.out.println(calculateHeight(tree));
    }
}
